// https://leetcode.cn/problems/distance-between-bus-stops/

#include <iostream>
#include <vector>
#include <string>


using namespace std;

class Solution {
public:
	// 每次距离递增的解法
    int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
        // int sum = getLen(distance[0], distance[distance.size() - 1]);
        // int dis = getLen(distance[start], distance[destination - 1]);
		if(start == destination) return 0;
		int sum = 0;
		int dis = 0;
		int i = 0;
		int len = distance.size();
		if(start < destination){
			while(i < len){
				sum += distance[i];
				if(i >= start && i < destination){
					dis += distance[i];
				}
				i++;
			}
		}else{
			while(i < len){
				sum += distance[i];
				if(i < start && i >= destination){
					dis += distance[i];
				}
				i++;
			}
		}

		return min(dis, sum - dis);
    }
	
	int getLen(int start, int end){
		int count = (end - start + 1) / 2;
		int sum = (end + start) * count;
		if((end - start + 1) % 2 == 1){
			sum += (end + start) / 2;
		}
		return sum;
	}
};
int main(){
	Solution so;
	vector<int> s = {7,10,1,12,11,14,5,0};
	auto res = so.distanceBetweenBusStops(s, 7, 2);
	cout << res << endl;
	return 0;
}